﻿using System;
using System.Collections.Generic;
using System.Drawing;

using RexToy;
using RexToy.L10N;
using RexToy.UI.WinForm.Pmlx;
using RexToy.UI.WinForm.Controls;

using MGAS.Model.Entity;

namespace MGAS.UI.Controls
{
    public class Sensor : UserControl
    {
        [PmlxBoundControl("Name")]
        protected System.Windows.Forms.Label _lblName;

        [PmlxBoundControl("Value")]
        protected System.Windows.Forms.Label _lblValue;

        [PmlxBoundControl("Unit")]
        protected System.Windows.Forms.Label _lblUnit;

        [PmlxBoundControl("Status")]
        protected System.Windows.Forms.Label _lblStatus;

        [PmlxBoundControl("history")]
        protected HistoryChart _history;

        [PmlxBoundControl("dashboard")]
        protected SensorDataList _dashboard;

        private PressureStatus _status;
        public PressureStatus Status
        {
            get { return _status; }
        }

        public byte AddressId
        {
            get;
            set;
        }

        public string PipeName
        {
            get { return _lblName.Text; }
            set { _lblName.Text = value; }
        }

        public Sensor()
        {
            this._lblStatus.DoubleClick += new EventHandler(_lblStatus_DoubleClick);
        }

        void _lblStatus_DoubleClick(object sender, EventArgs e)
        {
            FrmHistory frm = new FrmHistory();
            frm.SensorID = this.AddressId;
            frm.ShowDialog();
        }

        public void UpdateData(int val, PressureStatus status, PressureUnit unit)
        {
            var l10n = L10NContext.GetLocalization();

            _status = status;
            _lblStatus.Text = l10n.Localize(EnumEx.GetDescription(status));
            _lblUnit.Text = l10n.Localize(EnumEx.GetDescription(unit));
            _lblValue.Text = val.ToString();

            _dashboard.Append(val, status, unit);

            _history.UpdateData(val);
            switch (status)
            {
                case PressureStatus.Normal:
                    SetColor(Color.LightGreen);
                    break;

                case PressureStatus.High:
                    SetColor(Color.Orange);
                    break;

                case PressureStatus.ExHigh:
                    SetColor(Color.IndianRed);
                    break;

                case PressureStatus.Low:
                    SetColor(Color.LightBlue);
                    break;

                case PressureStatus.ExLow:
                    SetColor(SystemColors.Highlight);
                    break;

                case PressureStatus.Error:
                    SetColor(Color.MediumPurple);
                    break;

                case PressureStatus.Unknown:
                    SetColor(Color.LightPink);
                    break;
            }
        }

        private void SetColor(Color color)
        {
            _lblStatus.BackColor = color;
            _lblName.BackColor = color;
            _lblValue.BackColor = color;
            _lblUnit.BackColor = color;
        }
    }
}
